package com.mate.cloud.sensitive.config;


import com.mate.cloud.sensitive.annotation.EnableSensitive;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置类选择器
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/2/11 16:49
 * @updateUser: MI
 * @updateTime: 2025/2/11 16:49
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class SensitiveConfigurationSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {

        AnnotationAttributes enableSensitive = AnnotationAttributes.fromMap(
                importingClassMetadata.getAnnotationAttributes(EnableSensitive.class.getName(), false));
        if (enableSensitive == null) {
            throw new IllegalArgumentException(
                    "@EnableSensitive is not present on importing class " + importingClassMetadata.getClassName());
        }
        boolean security = enableSensitive.getBoolean("security");
        boolean sensitive = enableSensitive.getBoolean("sensitive");
        List<String> configuration = new ArrayList<>(2);
        if (security) {
            configuration.add(SecurityAutoConfiguration.class.getName());
        }
        if (sensitive) {
            configuration.add(SensitiveAutoConfiguration.class.getName());
        }
        if (configuration.isEmpty()) {
            return null;
        }

        return configuration.toArray(new String[configuration.size()]);
    }

}
